package com.longrise.co.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.view.View;

import com.ly.common.dialog.DialogOnClickListener;
import com.ly.common.dialog.MDAlertDialog;

/**
 * Created by Administrator on 2017/4/7.
 */

public class PermissionHelper {

    //判断是否有相机权限
    public static boolean checkCameraPermissions(Context context) {
        if (!selfPermissionGranted(context, Manifest.permission.CAMERA)) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constants.PERMISSION);
            return false;
        }
        return checkPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    //判断是否有定位权限
    public static boolean checkLocPermissions(Context context) {
        return checkPermissions(context, Manifest.permission.ACCESS_FINE_LOCATION);
    }

    //判断是否有电话权限
    public static boolean checkPhonePermissions(Context context) {
        return checkPermissions(context, Manifest.permission.CALL_PHONE);
    }

    //判断是否有权限
    public static boolean checkPermissions(Context context, String permission) {
        if (!selfPermissionGranted(context, permission)) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{permission}, Constants.PERMISSION);
            return false;
        }
        return true;
    }

    //没有权限时弹框提示需要权限
    public static void showPermissionDialog(final Context context, String msg, final boolean isClose) {
        MDAlertDialog mdAlertDialog = new MDAlertDialog.Builder(context)
                                                        .setContentText(msg)
                                                        .setLeftButtonText("取消")
                                                        .setRightButtonText("设置权限")
                                                        .setTitleText("权限设置提醒")
                                                        .setOnclickListener(new DialogOnClickListener() {
                                                            @Override
                                                            public void clickLeftButton(View view) {
                                                                if (isClose) {
                                                                    ((Activity) context).finish();
                                                                }
                                                            }

                                                            @Override
                                                            public void clickRightButton(View view) {
                                                                /**
                                                                 * 设置界面权限被修改后，程序会自动重启。是否会影响到界面的显示和数据保存？
                                                                 */
                                                                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                                                intent.setData(Uri.parse("package:" + context.getPackageName()));
                                                                context.startActivity(intent);
                                                                if (isClose) {
                                                                    ((Activity) context).finish();
                                                                }
                                                            }
                                                        })
                                                       .build();
        mdAlertDialog.show();

      /*  UiUtils.showDialog(context, R.mipmap.warn, null, msg, "取消", "设置权限", new View.OnClickListener() {
            @Override
            public void onClick(View v) {//关闭当前界面
                if (isClose) {
                    ((Activity) context).finish();
                }
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {// 跳转设置界面
                *//**
                 * 设置界面权限被修改后，程序会自动重启。是否会影响到界面的显示和数据保存？
                 *//*
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
                if (isClose) {
                    ((Activity) context).finish();
                }
            }
        });*/
    }

    //判断权限是否允许
    public static boolean selfPermissionGranted(Context context, String permission) {
        int targetSdkVersion = getTargetVersion(context);

        // For Android < Android M, self permissions are always granted.
        boolean result = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            if (targetSdkVersion >= Build.VERSION_CODES.M) {
                // targetSdkVersion >= Android M, we can
                // use Context#checkSelfPermission
                result = context.checkSelfPermission(permission)
                        == PackageManager.PERMISSION_GRANTED;
            } else {
                // targetSdkVersion < Android M, we have to use PermissionChecker
                result = PermissionChecker.checkSelfPermission(context, permission)
                        == PermissionChecker.PERMISSION_GRANTED;
            }
        }

        return result;
    }

    //获取当前编译版本
    private static int getTargetVersion(Context context) {
        int targetSdkVersion = 0;
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            targetSdkVersion = info.applicationInfo.targetSdkVersion;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return targetSdkVersion;
    }
}
